package br.ufrpe.bsi.pvs.database.roupa;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import br.ufrpe.bsi.pvs.database.Conexao;
import br.ufrpe.bsi.pvs.database.DAOFactory;
import br.ufrpe.bsi.pvs.model.roupa.Notas;
import br.ufrpe.bsi.pvs.model.roupa.Roupa;

public class NotasDAOImpl implements NotasDAO {

	@Override
	public void salvar(Notas object) {
		String insert = "insert into notas (?, ?, ?)";
		try (Connection con = Conexao.getConnection()) {
			PreparedStatement ps = con.prepareStatement(insert);
			ps.setInt(1, object.getRoupa().getId());
			ps.setInt(2, object.getTipoCorporeo().getId());
			ps.setInt(3, object.getNota());

			ps.execute();
			ps.close();
		} catch (Exception e) {
			System.err.println("[ADD PROBLEM] cant add notas: " + e);
		}
	}

	@Override
	public void remover(Notas object) {
		String delSQL = "delete from notas where roupa_id = ? and tipo_corporeo_id = ?";
		try (Connection con = Conexao.getConnection()) {
			PreparedStatement ps = con
					.prepareStatement(delSQL);
			ps.setInt(1, object.getRoupa().getId());
			ps.setInt(2, object.getTipoCorporeo().getId());

			ps.execute();
			ps.close();
		} catch (Exception e) {
			System.err.println("[DEL PROBLEM] cant del nota: " + e);
		}
	}

	@Override
	public void remover(Serializable id) {
		
	}

	@Override
	public List<Notas> listar() {
		List<Notas> notas = new ArrayList<>();
		try (Connection con = Conexao.getConnection()) {
			PreparedStatement ps = con.prepareStatement("select * from notas");
			ResultSet rs = ps.executeQuery();

			while (rs.next())
				notas.add(buildNota(rs));
 
			ps.close();
		} catch (Exception e) {
			System.err.println("[LIST PROBLEM] cant list notas: " + e);			
		}

		return notas;
	}

	@Override
	public Notas pesquisar(Serializable id) {
		return null;
	}

	@Override
	public Notas pesquisar(String name) {
		return null;
	}

	@Override
	public List<Notas> getNotasRoupa(Roupa roupa) {
		List<Notas> notas = new ArrayList<>();
		try (Connection con = Conexao.getConnection()) {
			PreparedStatement ps = con.prepareStatement("select * from notas where roupa_id = ?");
			ps.setInt(1, roupa.getId());
			
			ResultSet rs = ps.executeQuery();

			while (rs.next())
				notas.add(buildNota(rs));
 
			ps.close();
		} catch (Exception e) {
			System.err.println("[LIST PROBLEM] cant list notas: " + e);
			e.printStackTrace();
		}

		return notas;
	}

	@Override
	public List<Notas> getNotasRoupa(Serializable id) {
		List<Notas> notas = new ArrayList<>();
		try (Connection con = Conexao.getConnection()) {
			PreparedStatement ps = con.prepareStatement("select * from notas where roupa_id = ?");
			ps.setInt(1, (Integer) id);
			
			ResultSet rs = ps.executeQuery();

			while (rs.next())
				notas.add(buildNota(rs));
 
			ps.close();
		} catch (Exception e) {
			System.err.println("[LIST PROBLEM] cant list notas: " + e);
			e.printStackTrace();
		}

		return notas;
	}
	
	/** recebe um result set de uma query do banco e constroi o objeto */
	private Notas buildNota(ResultSet rs) throws Exception {
		Notas n = new Notas();
		
		RoupaDAO roupadao = DAOFactory.getRoupaDAO();
		n.setRoupa(roupadao.pesquisar(rs.getInt("roupa_id")));
		
		TipoCorporeoDAO tipocorporeodao = DAOFactory.getTipoCorporeoDAO();
		n.setTipoCorporeo(tipocorporeodao.pesquisar(rs.getInt("tipo_corporeo_id")));
		
		n.setNota(rs.getInt("nota"));
		
		return n;
	}

}
